package com.alimama.mdrill.jdbc;

import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;


public class MdrillConnection implements java.sql.Connection {
	private boolean isClosed = true;
	  private SQLWarning warningChain = null;

	  private String httpurl="";
	  private static final String URI_PREFIX = "jdbc:mdrill://";
	  private static final String URI_PREFIX_OLD = "jdbc:higo://";


	  public MdrillConnection(String uri, Properties info) throws SQLException {
	    if (!uri.startsWith(URI_PREFIX)&&!uri.startsWith(URI_PREFIX_OLD)) {
	      throw new SQLException("Invalid URL: " + uri, "08S01");
	    }
	    if (uri.startsWith(URI_PREFIX_OLD)) {
	    	httpurl = uri.substring(URI_PREFIX_OLD.length());
	    }else{
		    httpurl = uri.substring(URI_PREFIX.length());
	    }
	    
	    if (httpurl.isEmpty()) {
		      throw new SQLException("Invalid URL: " + uri, "08S01");
	      } 

	      isClosed = false;
	  }


	
	public Statement createStatement() throws SQLException {
		 if (isClosed) {
		      throw new SQLException("Can't create Statement, connection is closed");
		    }
		    return new MdrillStatement(this.httpurl);
	}

	
	public PreparedStatement prepareStatement(String sql) throws SQLException {
		return new MdrillPreparedStatement(this.httpurl, sql);
	}

	
	public CallableStatement prepareCall(String sql) throws SQLException {
		throw new SQLException("Method not supported");
	}

	
	public String nativeSQL(String sql) throws SQLException {
		throw new SQLException("Method not supported");
	}

	
	public boolean getAutoCommit() throws SQLException {
		return true;
	}


	
	public boolean isClosed() throws SQLException {
		return isClosed;
	}

	
	public DatabaseMetaData getMetaData() throws SQLException {
	    return new MdrillDatabaseMetaData(this.httpurl);

	}

	
	public void setReadOnly(boolean readOnly) throws SQLException {
		throw new SQLException("Method not supported");
	}

	
	public boolean isReadOnly() throws SQLException {
		return false;
	}

	
	public void setCatalog(String catalog) throws SQLException {
		throw new SQLException("Method not supported");
	}

	
	public String getCatalog() throws SQLException {
		return "";
	}

	
	public void setTransactionIsolation(int level) throws SQLException {
		throw new SQLException("Method not supported");
		
	}

	
	public int getTransactionIsolation() throws SQLException {
		return Connection.TRANSACTION_NONE;
	}

	
	public SQLWarning getWarnings() throws SQLException {
		return warningChain;
	}

	
	public void clearWarnings() throws SQLException {
		 warningChain = null;
	}

	
	public Statement createStatement(int resultSetType, int resultSetConcurrency)
			throws SQLException {
		throw new SQLException("Method not supported");
	}

	
	public PreparedStatement prepareStatement(String sql, int resultSetType,
			int resultSetConcurrency) throws SQLException {
		return new MdrillPreparedStatement(this.httpurl, sql);
	}

	
	public CallableStatement prepareCall(String sql, int resultSetType,
			int resultSetConcurrency) throws SQLException {
		throw new SQLException("Method not supported");
	}

	
	public int getHoldability() throws SQLException {
		throw new SQLException("Method not supported");
	}
	
	
	public Map<String, Class<?>> getTypeMap() throws SQLException {
		throw new SQLException("Method not supported");
	}


	
	public Statement createStatement(int resultSetType,
			int resultSetConcurrency, int resultSetHoldability)
			throws SQLException {
		throw new SQLException("Method not supported");
	}

	

	
	public CallableStatement prepareCall(String sql, int resultSetType,
			int resultSetConcurrency, int resultSetHoldability)
			throws SQLException {
		throw new SQLException("Method not supported");
	}

	
	public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
			throws SQLException {
		return new MdrillPreparedStatement(this.httpurl, sql);
	}

	
	public PreparedStatement prepareStatement(String sql, int[] columnIndexes)
			throws SQLException {
		throw new SQLException("Method not supported");
	}

	
	public PreparedStatement prepareStatement(String sql, String[] columnNames)
			throws SQLException {
		throw new SQLException("Method not supported");
	}

	
	
	public boolean isValid(int timeout) throws SQLException {
		throw new SQLException("Method not supported");
	}

	
	
	public String getClientInfo(String name) throws SQLException {
		throw new SQLException("Method not supported");
	}

	
	public Properties getClientInfo() throws SQLException {
		throw new SQLException("Method not supported");
	}

	
	
	public Struct createStruct(String typeName, Object[] attributes)
			throws SQLException {
		throw new SQLException("Method not supported");
	}
	
	
	public Clob createClob() throws SQLException {
		throw new SQLException("Method not supported");
	}

	
	public Blob createBlob() throws SQLException {
		throw new SQLException("Method not supported");
	}

	
	public NClob createNClob() throws SQLException {
		throw new SQLException("Method not supported");
	}

	
	public SQLXML createSQLXML() throws SQLException {
		throw new SQLException("Method not supported");
	}
	
	
	public void setClientInfo(String name, String value)
			throws SQLClientInfoException {
		throw new SQLClientInfoException("Method not supported", null);
	}

	
	public void setClientInfo(Properties properties)
			throws SQLClientInfoException {
		throw new SQLClientInfoException("Method not supported", null);
	}
	
	
	public Array createArrayOf(String typeName, Object[] elements)
			throws SQLException {
		throw new SQLException("Method not supported");
	}

	
	public <T> T unwrap(Class<T> iface) throws SQLException {
		throw new SQLException("Method not supported");
	}

	
	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		throw new SQLException("Method not supported");
	}
	
	
	public void setAutoCommit(boolean autoCommit) throws SQLException {
		throw new SQLException("Method not supported");
	}

	
	
	public void commit() throws SQLException {
		throw new SQLException("Method not supported");
	}

	
	public void rollback() throws SQLException {
		throw new SQLException("Method not supported");
	}

	
	public void close() throws SQLException {
        isClosed = true;
	}
	
	
	public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
		throw new SQLException("Method not supported");
	}

	
	public void setHoldability(int holdability) throws SQLException {
		throw new SQLException("Method not supported");
	}

	
	public Savepoint setSavepoint() throws SQLException {
		throw new SQLException("Method not supported");
	}

	
	public Savepoint setSavepoint(String name) throws SQLException {
		 throw new SQLException("Method not supported");
	}

	
	public void rollback(Savepoint savepoint) throws SQLException {
		throw new SQLException("Method not supported");
	}

	
	public void releaseSavepoint(Savepoint savepoint) throws SQLException {
		throw new SQLException("Method not supported");
	}
	
	
	public PreparedStatement prepareStatement(String sql, int resultSetType,
			int resultSetConcurrency, int resultSetHoldability)
			throws SQLException {
		throw new SQLException("Method not supported");
	}


	
	public void abort(Executor arg0) throws SQLException {
		// TODO Auto-generated method stub
		
	}


	
	public int getNetworkTimeout() throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}


	
	public String getSchema() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}


	
	public void setNetworkTimeout(Executor arg0, int arg1) throws SQLException {
		// TODO Auto-generated method stub
		
	}


	
	public void setSchema(String arg0) throws SQLException {
		// TODO Auto-generated method stub
		
	}
}
